package com.example.mouse;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.mouse.R;
import com.example.mouse.SocketClient;
import com.example.mouse.TouchPadView;

public class MainActivity extends AppCompatActivity {
    private SocketClient socketClient;
    private TouchPadView touchPadView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        touchPadView = findViewById(R.id.touchpad);
        EditText etIp = findViewById(R.id.et_ip);
        Button btnConnect = findViewById(R.id.btn_connect);

        socketClient = new SocketClient();

        btnConnect.setOnClickListener(v -> {
            String ip = etIp.getText().toString();
            int port = 8888;

            socketClient.connect(ip, port,
                    () -> Toast.makeText(this, "连接成功", Toast.LENGTH_SHORT).show(),
                    () -> Toast.makeText(this, "连接失败", Toast.LENGTH_SHORT).show()
            );

            touchPadView.setSocketClient(socketClient);
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        socketClient.disconnect();
    }
}